package com.yanqu.road.server.cmd.chefduel.process2;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.exception.BusinessException;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.chefduel.ChefDuelProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.chefduel.ChefDuelActivityMgr;
import com.yanqu.road.server.manger.activity.chefduel.ChefDuelMgr;
import com.yanqu.road.server.manger.activity.chefduel.OtherActivityChefDuel2;
import com.yanqu.road.server.manger.activity.chefduel.OtherActivityChefDuelActivityMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

import java.util.Map;

@Cmd(code = Protocol.S_CHEF_DUEL2_SEARCH_ONE, desc = "搜索摊位信息")
public class ChefDuel2SearchUserCmd extends AbstractCommand {

	int activityId;
	String searchId;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		ChefDuelProto.ChefDuel2SearchOneReqMsg reqMsg = ChefDuelProto.ChefDuel2SearchOneReqMsg.parseFrom(packet.getBytes());
		activityId = reqMsg.getActivityId();
		searchId = reqMsg.getSearchId();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		ActivityInfo activityInfo = ChefDuelActivityMgr.getOpenActivityInfo(activityId);
		if(activityInfo == null){
			ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
			return;
		}
		try {
			//因扩大了赴宴范围，得先确定查找的玩家归属哪个活动
			Long aliasId = Long.valueOf(searchId);
			Map<Long, Long> userServerMap = UserMgr.getUserServerByUserAliasId(aliasId, 0);
			if (userServerMap.isEmpty()) {
				ret = GameErrorCode.E_CHEF_DUEL2_USER_NOT_FUND;
				return;
			}
			long serverId = 0;
			long userId = 0;
			for (Map.Entry<Long, Long> entry : userServerMap.entrySet()) {
				if (activityInfo.getServerIdList2().contains(entry.getValue())) {
					serverId = entry.getValue();
					break;
				}
			}
			if (activityInfo.getServerIdList().contains(serverId)) {
				ret = ChefDuelMgr.sendSearchUserMsg(activityId, player.getUserId(), searchId);
			} else {
				for (Map.Entry<Integer, OtherActivityChefDuel2> entry : OtherActivityChefDuelActivityMgr.getChefDuel2Map().entrySet()) {
					ActivityInfo info = entry.getValue().getActivityInfo();
					if (ActivityMgr.activityInTime(info) && info.getServerIdList().contains(serverId)) {
						OtherActivityChefDuel2 chefDuel2 = OtherActivityChefDuelActivityMgr.getOtherActivityChefDuel2(info.getActivityId());
						if (chefDuel2 != null) {
							ret = chefDuel2.sendSearchUserMsg(player.getUserId(), searchId);
						}
					}
				}
			}
		}catch (Exception e){
			if(e instanceof BusinessException){
				ret = ((BusinessException)e).getCode();
			}
		}
	}

	@Override
	public void sendData(GamePlayer player) {
		if(ret != 0){
			ChefDuelProto.ChefDuel2SearchOneRespMsg.Builder respMsg = ChefDuelProto.ChefDuel2SearchOneRespMsg.newBuilder();
			respMsg.setRet(ret);
			player.sendPacket(Protocol.U_CHEF_DUEL2_SEARCH_ONE, respMsg);
		}
	}
}
